﻿using DevExpress.XtraEditors;
using System.Windows.Forms;
using System;
using System.Data;
using System.Linq;
using System.Collections.Generic;
using DevExpress.XtraEditors.Controls;
using System.ComponentModel;
using Mota.EditModule;

namespace Mota.XMLControls
{
    public class DXGuiRadioGroup:GuiRadioGroup
    {
        public DXGuiRadioGroup()
        {
            visualControl = new DBRadioGroup();
            DBRadioGroup lue = visualControl as DBRadioGroup;
        }

        
        public override void AddStaticItem(string value)
        {
            RadioGroupItemCollection items = (visualControl as DBRadioGroup).Properties.Items;
            items.Add(new RadioGroupItem(items.Count,value));
        }
        public override string Text
        {
            get
            {
                return (visualControl as DBRadioGroup).Text;
            }
            set
            {
                (visualControl as DBRadioGroup).Text = value;
            }
        }
        public override object Binding
        {
            get
            {
                return (visualControl as DBRadioGroup).DataSource;
            }
            set
            {
                // set available values to the list using BindingSource
                if (value is BindingSource)
                {
                    var radioGroup = (visualControl as DBRadioGroup);

                    radioGroup.ValueMember = XmlAttributes["_KeyField"];
                    radioGroup.DisplayMember = XmlAttributes["_Fields"].Split(',').Last();
                    radioGroup.DataSource = value;
                }
            }
        }

        public override void SetValue(object valueBinding)
        {
            if ((visualControl as DBRadioGroup).DataSource != null)
            {
                DBRadioGroup rg = (visualControl as DBRadioGroup);
                //TODO:correct to right way for storing valueBinding
                rg.Tag = valueBinding;
                CurrencyManager cm = (valueBinding as BindingSource).CurrencyManager;
                PropertyDescriptor pdValue = cm.GetItemProperties()[XmlAttributes["XMLAttribute"]];
                if (pdValue != null)
                {
                    object currValue = pdValue.GetValue(cm.Current);
                    int curItem = rg.Properties.Items.GetItemIndexByValue(currValue);
                    if (curItem >-1)
                    {
                        rg.SelectedIndex = curItem;
                    }

                    rg.Properties.SelectedIndexChanged += rg_SelectedIndexChanged;
                }
            }
        }

        void rg_SelectedIndexChanged(object sender, EventArgs e)
        {
            CurrencyManager cm = ((visualControl as DBRadioGroup).Tag as BindingSource).CurrencyManager;
            PropertyDescriptor pdValue = cm.GetItemProperties()[XmlAttributes["XMLAttribute"]];
            if (pdValue != null)
            {
                var rg = (sender as DBRadioGroup);
                int selectedIndex = rg.SelectedIndex;
                if(selectedIndex>-1)
                    pdValue.SetValue(cm.Current, rg.Properties.Items[selectedIndex].Value);
                else
                    pdValue.SetValue(cm.Current, null );
            }
            this.ChangeEventCall(null);
        }

        public override void Refresh(ConnectionConfigs configs, BindingSource mainBs)
        {
            this.Rfsh(mainControls, mainBs, configs);
        }

        public override object GetValue()
        {
            return (this.visualControl as DBRadioGroup).SelectedIndex;
        }
    }
}
